﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using JTicket.Printer;
namespace JTicket.UI.Extention.IHandler
{
    public class TicketOrderHandler : IHttpHandler
    {
        #region IHttpHandler Members
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string thcode = request.QueryString["ThCode"].ToString();
            string subcode = request.QueryString["SubCode"].ToString();
            string titlecode = request.QueryString["TitleCode"].ToString();
            string seatList = request.QueryString["SeatList"].ToString();
            string usertxt = request.QueryString["UserTxt"].ToString();
            string usertel = request.QueryString["UserTel"].ToString();
            string userAddress = request.QueryString["UserAddress"].ToString();
            string userUnit = request.QueryString["UserUnit"].ToString();
            string Flag = Common.StringHelp.GenerateFileName();
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            Model.D_OperInfo oper = bllOper.Oper();
            Model.SaleParm sale = new Model.SaleParm();
            List<string> aList = new List<string>();
            string[] arr = seatList.Replace("G", "").Split('|');
            aList = Common.CheckCode.ListBarCode(arr.Length - 1);
            string opercode = oper.OperCode;
            string ordercode = string.Format("{0}", Flag);
            string saleip = HttpContext.Current.Request.UserHostAddress;
            bool res = false;
            if (arr.Length - 1 > 0)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    sale.AreaCode = subcode;
                    sale.ICCode = "";
                    sale.IsSend = 0;
                    sale.Msg = "";
                    sale.PayModel = "现金";
                    sale.SaleModel = "";
                    sale.Sixid = arr[i];
                    sale.SumAmt = 0.0M;
                    sale.SumCount = 0;
                    sale.TaoCode = "";
                    sale.ThCode = thcode;
                    sale.TitleCode = titlecode;
                    sale.UnitCode = userUnit;
                    sale.UserAddress = userAddress;
                    sale.UserName = usertxt;
                    sale.UserTel = usertel;
                    sale.Zhekou = "10.0";
                    res = BLL.H_OrderBal.OrderBookTicket(sale, opercode, saleip, aList[i], ordercode, "", Flag, "预订");
                }
            }
            if (res)
            {
                response.Write("预订成功！");
                response.End();
            }
            response.Write("预订失败！");
        }
        #endregion
    }

    public class TicketOrderGetHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            bool b = false;
            StringBuilder sb = new StringBuilder();
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            BLL.D_Tax bllTax = new BLL.D_Tax();
            string Invodio = "无";
            string thcode = bllOper.Oper().ThCode;
            string ordercode = request.QueryString["ordercode"].ToString();
            string username = request.QueryString["username"].ToString();
            string usertel = request.QueryString["usertel"].ToString();
            string titlecode = request.QueryString["titlecode"].ToString();
            string amt = request.QueryString["amt"].ToString();
            string cout = request.QueryString["cout"].ToString();
            Model.SaleParm sale = new Model.SaleParm();

            sale.ThCode = thcode;
            sale.UserName = username;
            sale.UserTel = usertel;
            sale.TitleCode = titlecode;
            if (BLL.D_Tax.Exists(thcode))
            {
                Model.D_Tax Tax = bllTax.GetModel(thcode);
                if (!Tax.fpsy.Equals(Tax.fpsz))
                {
                    Invodio = Tax.fpsy;
                }
            }
            //生成发票号。
            if (Invodio != "无")
            {
                Print.BuildTaxU(username, amt, int.Parse(cout));
            }
            b = DAL.H_OrderBal.OrderBookTicket(sale,bllOper.Oper().OperCode,
                          request.UserHostAddress, "",
                            ordercode, Invodio, "", "预订取票");
            if (b)
            {
                List<Model.L_OrderBal> orderbal =
                    BLL.H_OrderBal.GetOrderInfoList
                    (ordercode, usertel);
                if (orderbal.Count > 0)
                {
                    for (int i = 0; i <= orderbal.Count - 1; i++)
                    {
                        sb.Append(Print.MakeOrderGetCmd
                                               (orderbal[i].ordercode, orderbal[i].concertcode, orderbal[i].ThCode, orderbal[i].BarCode));
                        sb.Append("|");
                    }
                }
                response.Write(sb.ToString().TrimEnd('|'));
                response.End();
            }
            else
                response.Write("-1");
        }
    }
}
